﻿//---------------------------------------------------------------------
//  Copyright (c) 2012-2013 Daun Project.  All rights reserved.
//  http://daun.codeplex.com
//  http://www.daun-project.com
//---------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Daun.Cores;
using Daun.Cores.Entities;
namespace Daun.Forms
{
    public partial class DocumentForm : Form
    {
        private MongoEngine _engine;

        public string CollectionName { set; get; }
        public string DatabaseName { set; get; }
        public string MongoPath { set; get; }
        public FormMode Mode { set; get; }
        public List<Daun.Cores.Entities.Document> Documents { set; get; }
        public MongoEngine DBEngine
        {
            set
            {
                _engine = value;
            }
        }
        public string FormTitle
        {
            set
            {
                this.Text = value;
            }
        }
        public DocumentProperty SelectedObject
        {
            get
            {
                return (DocumentProperty)this.propertyGrid1.SelectedObject;
            }
        }
        public DocumentForm()
        {
            InitializeComponent();
        }

        private void Initial()
        {
            DocumentProperty props = new DocumentProperty();
            if (Mode == FormMode.New)
            {
                List<Document> list = _engine.GetCollectionScheme(DatabaseName, CollectionName);                
                props.Add(list);                
            }
            else
            {
                foreach (var doc in Documents)
                {
                    if (doc.Key == "_id" && doc.DocumentType == "ObjectId")
                        continue;
                    props.Add(doc.Key, doc.Value,doc.DocumentType);
                }
            }
            props.DocumentCategory = string.Format("{0}/{1}", DatabaseName, CollectionName);
            propertyGrid1.SelectedObject = props;            
        }

        private void DocumentForm_Load(object sender, EventArgs e)
        {
            Initial();            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

    }
}
